﻿using Android.App;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using static Android.Media.MediaPlayer;
using static Android.Views.View;

namespace YuRadio
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        MediaPlayer mp;
        TextView textMessage;
        RecyclerView recyclerView;
        StationAdapter stAdapter;
        Frm_curd frmCurd;
        
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);


            textMessage = FindViewById<TextView>(Resource.Id.message);
            recyclerView = FindViewById<RecyclerView>(Resource.Id.stationList);
            recyclerView.SetLayoutManager(new LinearLayoutManager(this));

            List<Station> stations = StationManager.GetList();
            stAdapter = new StationAdapter(stations.ToArray(),this.mp);
            recyclerView.SetAdapter(stAdapter);
            
            frmCurd = Frm_curd.Init(this, Resource.Id.layEdit);
            frmCurd.OnChanged += FrmCurd_OnAdded;
        }

        private void FrmCurd_OnAdded(object sender, AddStationEventArgs e)
        {
            if (e.AddorEdit == 1)
                stAdapter.AddItem(e.Station);
            else
                stAdapter.UpdateItem(e.Station);
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.options,menu);
            return base.OnPrepareOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            if (item.ItemId == Resource.Id.del_station)
            {
                deleteCurrent();
                return true;
            }
            if (item.ItemId == Resource.Id.add_station)
            {
                frmCurd.AddNew();
                return true;
            }
            if (item.ItemId == Resource.Id.edit_station)
            {
                Station station = stAdapter.getCurrent();
                if (station != null)
                {
                    frmCurd.Edit(station);
                }
                return true;
            }
            if (item.ItemId == Resource.Id.stop)
            {
                stAdapter.Stop();
                return true;
            }
            return base.OnOptionsItemSelected(item);
        }


       
        private void deleteCurrent()
        {
            Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
            Android.App.AlertDialog alert = dialog.Create();
            alert.SetTitle("Please Confirm");
            alert.SetMessage("要删除吗?");
            alert.SetButton("OK", (c, ev) =>
            {
                this.stAdapter.DeleteCurrent();
            });
            alert.SetButton2("Cancel", (c, ev) => { });
            alert.Show();
        }
    }
}

